package users;

import io.UserAccess;
import io.UserHasConfRoleAccess;

import java.util.List;

import ui.MainPage;

/**
 * A class primarily for storing information after a database reads
 * in user information for the first time.  Rather than having to
 * read from the database constantly to do functions, the software
 * can use this local data object instead.
 * @author Aaron Chen
 * @author Christopher M. Augg Moddified addRole to work with accessUserConfrole()
 * @version March 20, 2014
 */
public class User {

  /** A unique identification number for each user.  Used primarily
   * for database lookup tables. */
  private long my_id;

  /** The email address of the registered user.  This must be provided
   * during registration and cannot be blank or otherwise invalid. */
  private String my_email;

  /** The first name of the user. */
  private String my_first_name;

  /** The last name of the user. */
  private String my_last_name;

  /** The list of roles that the user has per conference, if any. */
  private List<Role> my_roles;
  

  /**
   * Constructs a new User data object.
   * @param the_userdata  A list of user data.
   * @param the_roles     The roles that the user is associated with.
   */
  public User(final List<String> the_userdata, final List<Role> the_roles) {
    my_id = Long.parseLong(the_userdata.get(UserAccess.USER_ID));
    my_first_name = the_userdata.get(UserAccess.USERFIRSTNAME);
    my_last_name = the_userdata.get(UserAccess.USERLASTNAME);
    my_email = the_userdata.get(UserAccess.USER_EMAIL);
    my_roles = the_roles;
  }
  
  /**
   * Constructs a new User data object with minimal data.
   * @param the_first The first name.
   * @param the_last  The last name.
   * @param the_email The email address.
   */
  public User(final String the_first, final String the_last, final String the_email) {
    my_first_name = the_first;
    my_last_name = the_last;
    my_email = the_email;
  }
  
  /**
   * Returns the identification number unique to the user.
   * @return The ID.
   */
  public long getID() {
    return my_id;
  }
  
  /**
   * Sets the identification number unique to the user.
   * @param the_id The new ID number.
   */
  public void setID(final long the_id) {
    my_id = the_id;
  }

  /**
   * Returns the user's email address.
   * @return The email address.
   */
  public String getEmail() {
    return my_email;
  }

  /**
   * Returns the user's first name.
   * @return The first name.
   */
  public String getFirstName() {
    return my_first_name;
  }

  /**
   * Returns the user's last name.
   * @return The last name.
   */
  public String getLastName() {
    return my_last_name;
  }
  
  /**
   * Returns the user's full name.
   * @return The full name.
   */
  public String getFullName() {
    return my_first_name + " " + my_last_name;
  }

  /**
   * Returns the list of roles that the user is associated with.
   * @return The list of roles.
   */
  public List<Role> getRoles() {
    return my_roles;
  }
  
  /**
   * Adds the role to the User's list of roles.
   * @param the_role The new role to be granted.
   */
  public void addRole(final Role the_role) {
    final UserHasConfRoleAccess db = MainPage.getDatabase().accessUserConfRole();
    final UserAccess uadb = MainPage.getDatabase().accessUser();
    my_roles.add(the_role);
    db.createUserRoles(the_role);
    uadb.editUser(this);
  }
}
